package com.myapp.war.beans;

import java.util.concurrent.atomic.AtomicInteger;

public class WarMagicBean {
	// 魔力值
	private AtomicInteger value = null;

	// 最后修改的时间
	private long lastUpdateTime = System.currentTimeMillis();

	public long getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public AtomicInteger getValue() {
		return value;
	}

	public void setValue(AtomicInteger value) {
		this.value = value;
	}

	/**
	 * 要减少的魔法值
	 * 
	 * @param value
	 *            要剪掉或者增加的魔力值
	 * 
	 * @return 计算魔法是否够扣除
	 */
	public boolean updateValue(int value) {
		// TODO 根据消灭的怪物数，计算魔法值的恢复情况
		boolean back = false;
		while (true) {
			int current = this.value.get();
			if ((current + value) > 0) {
				if (this.value.compareAndSet(current, (current + value))) {
					back = true;
					break;
				}
			} else {
				back = false;
				break;
			}
		}
		return back;
	}
}
